#include "window-properties.h"

void
window_properties_width_changed(GtkEntry *entry,
								gpointer data)
{
	struct window_properties_s *window = (struct window_properties_s *)data;
	double width;
	char *w_str;

	w_str = gtk_entry_get_text(window->entry_width);
	width = atof(w_str);

	shape_change_width(window->shape, width);
}

void
window_properties_text_changed(GtkEntry *entry,
								gpointer data)
{
	struct window_properties_s *window = (struct window_properties_s *)data;
	char *text;

	text = gtk_entry_get_text(window->entry_text);
	shape_change_text(window->shape, text);
}


void
window_properties_button_close_clicked(GtkButton *button,
									   gpointer data)
{
	struct window_properties_s *window = (struct window_properties_s *)data;
	gtk_widget_destroy(window->window);
	g_free(window);
}

void
window_properties_fill_color_set(GtkColorButton *button,
								 gpointer data)
{
	struct window_properties_s *window = (struct window_properties_s *)data;
	GdkColor color;
	char *color_str;

	gtk_color_button_get_color(button, &color);
	color_str = gdk_color_to_string(&color);
	strcpy(window->shape->fill, color_str);
	g_free(color_str);
}

void
window_properties_init(GdkWindow *drawing,
					   shape_t *shape)
{
	struct window_properties_s *window;
	char *tmp;

	window = g_malloc(sizeof(struct window_properties_s));

	window->shape = shape;
	window->label_width = gtk_label_new("Width");
	gtk_misc_set_alignment(window->label_width, 0.0, 0.0);
	window->label_height = gtk_label_new("Height");
	gtk_misc_set_alignment(window->label_height, 0.0, 0.0);
	window->label_text = gtk_label_new("Text");
	gtk_misc_set_alignment(window->label_text, 0.0, 0.0);

	window->entry_width = gtk_entry_new();
	tmp = g_strdup_printf("%f",shape->w);
	gtk_entry_set_text(window->entry_width, tmp);
	g_signal_connect(window->entry_width, "changed", window_properties_width_changed, window);
	window->entry_height = gtk_entry_new();
	tmp = g_strdup_printf("%f", shape->h);
	gtk_entry_set_text(window->entry_height, tmp);
	window->entry_text = gtk_entry_new();
//	if (window->shape->text)
//		gtk_entry_set_text(window->entry_width, window->shape->text);
	g_signal_connect(window->entry_text, "changed", window_properties_text_changed, window);

	window->button_close = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
	g_signal_connect(window->button_close, "clicked", window_properties_button_close_clicked, window);

	window->button_color = gtk_color_button_new();
	gtk_color_button_set_title(window->button_color, "Fill color");
	g_signal_connect(window->button_color, "color-set", window_properties_fill_color_set, window);

	window->box_main = gtk_vbox_new(FALSE, 2);
	gtk_box_pack_start(window->box_main, window->label_width, FALSE, FALSE, 2);
	gtk_box_pack_start(window->box_main, window->entry_width, FALSE, FALSE, 2);
	gtk_box_pack_start(window->box_main, window->label_height, FALSE, FALSE, 2);
	gtk_box_pack_start(window->box_main, window->entry_height, FALSE, FALSE, 2);
	gtk_box_pack_start(window->box_main, window->label_text, FALSE, FALSE, 2);
	gtk_box_pack_start(window->box_main, window->entry_text, FALSE, FALSE, 2);
	gtk_box_pack_start(window->box_main, window->button_color, FALSE, FALSE, 2);
	gtk_box_pack_start(window->box_main, window->button_close, FALSE, FALSE, 2);

	window->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title(window->window, "Properties");
	gtk_window_set_position(window->window, GTK_WIN_POS_CENTER);
	gtk_container_add(window->window, window->box_main);
	gtk_container_set_border_width(window->window, 2);
	gtk_widget_show_all(window->window);
}


